package com.distributed.tcp.controller;

import com.distributed.tcp.model.CommonResult;
import com.distributed.tcp.model.MessageForwardRequest;
import com.distributed.tcp.service.DeviceMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
@RequestMapping("/api/message")
@Tag(name = "消息转发接口", description = "设备消息发送及跨实例转发的API接口")
public class MessageController {
    @Resource
    private DeviceMessageService messageService;

    /**
     * 发送消息到设备
     */
    @PostMapping("/send")
    @Operation(summary = "发送消息到设备", description = "自动处理本地/跨实例转发")
    public CommonResult<Boolean> sendMessage(
            @Parameter(required = true, example = "device_123456", description = "设备ID")
            @RequestParam String deviceId,

            @Parameter(required = true, example = "测试消息", description = "消息内容")
            @RequestParam String message) {
        return messageService.sendMessageToDevice(deviceId, message);
    }

    /**
     * 处理转发消息（供其他实例调用）
     */
    @PostMapping("/forward")
    @Operation(summary = "处理转发消息", description = "接收其他实例的转发请求")
    public CommonResult<Boolean> forwardMessage(@RequestBody MessageForwardRequest request) {
        return messageService.handleForwardMessage(request);
    }
}
